home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: pstaite@vnet.ibm.com
- Newsgroups: comp.lang.c++
- Subject: Re: Callbacks using member functions
- Date: 16 Jan 1996 19:40:02 GMT
- Organization: IBM OS/2 Device Driver Development Rochester, MN
- Message-ID: <4dguui$m7k@locutus.rchland.ibm.com>
- References: <4dgkke$8mf@knot.queensu.ca>
- Reply-To: pstaite@vnet.ibm.com
- NNTP-Posting-Host: warpone.rchland.ibm.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4dgkke$8mf@knot.queensu.ca>, 3mal5@qlink.queensu.ca (Lepage Marc A) writes:
- >I am writing a MOTIF application, and I wish to use classes for the
- >various elements of my program. MOTIF is C, and uses callbacks. I want
- >to install member functions as the callbacks, but g++ complains, I
- >suspect because of the implicit 'this' parameter. Is there a way to get
- >this to work (ie, why can't the compiler straighten everything out)?
- >
- >I expect that I cannot use a member function as a callback, but wish to
- >confirm this before I go ahead and start using friend functions.
- >
- >So on a related note, if I use friends, what is the best way to make sure
- >I have access to the proper class instances, without the 'this' parameter?
-
- This is precisely why the compiler can't do it for you. Which instance,
- if any? From the C++ FAQ:
-
- ==============================================================================
-
- Q112: How do I pass a ptr to member fn to a signal handler, X event callback,
- etc?
-
- Don't.
-
- Because a member function is meaningless without an object to invoke it on, you
- can't do this directly (if The X Windows System was rewritten in C++, it would
- probably pass references to OBJECTS around, not just pointers to fns; naturally
- the objects would embody the required function and probably a whole lot more).
-
- As a patch for existing software, use a top-level (non-member) function as a
- wrapper which takes an object obtained through some other technique (held in a
- global, perhaps). The top-level function would apply the desired member
- function against the global object.
-
- E.g., suppose you want to call Fred::memfn() on interrupt:
-
- class Fred {
- public:
- void memfn();
- static void staticmemfn(); //a static member fn can handle it
- //...
- };
-
- //wrapper fn remembers the object on which to invoke memfn in a global:
- Fred* object_which_will_handle_signal;
- void Fred_memfn_wrapper() { object_which_will_handle_signal->memfn(); }
-
- main()
- {
- /* signal(SIGINT, Fred::memfn); */ //Can NOT do this
- signal(SIGINT, Fred_memfn_wrapper); //Ok
- signal(SIGINT, Fred::staticmemfn); //Also Ok
- }
-
- Note: static member functions do not require an actual object to be invoked, so
- ptrs-to-static-member-fns are type compatible with regular ptrs-to-fns (see ARM
- ["Annotated Reference Manual"] p.25, 158).
-
- ==============================================================================
-
-
- Phil Staite, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-
-